<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="dateRange"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetimerange"
        range-separator="~"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :editable="false"
        class="filter-item"
        @change="changeDate"
      />
      <el-select v-model="listQuery.logType" placeholder="类型" clearable class="filter-item" style="width: 120px;">
        <el-option v-for="item in logTypeOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>
      <el-input v-model="listQuery.logName" placeholder="名称" class="filter-item" clearable style="width: 200px;" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="类型" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.logType | logTypeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.logName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.logContent }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.logTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { getSyslog } from '@/api/syslog';
import waves from '@/directive/waves';
import Pagination from '@/components/Pagination';
import { parseTime } from '@/utils';

var that;

export default {
  name: 'Syslog',
  components: { Pagination },
  directives: { waves },
  filters: {
    logTypeFilter(item) {
      const logTypeKeyValue = that.logTypeOptions.reduce((acc, cur) => {
        acc[cur.key] = cur.label;
        return acc;
      }, {});
      return logTypeKeyValue[item];
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        logType: null,
        logName: '',
        beginTime: '',
        endTime: ''
      },
      dateRange: [],
      logTypeOptions: [{ label: '摄像头日志', key: 0 }, { label: '闸机日志', key: 1 }, { label: '数据中心接口日志', key: 2 }],
      type: null
    };
  },
  created() {
    that = this;
    const { params } = this.$route
    Object.keys(params).forEach(key => {
      this.type = params[key].split(':')[1];
    });
    this.dateRange.push(parseTime(new Date(), '{y}-{m}-{d}') + ' 00:00:00');
    this.dateRange.push(parseTime(new Date(), '{y}-{m}-{d}') + ' 23:59:59');
    this.listQuery.beginTime = this.dateRange[0] + ' 00:00:00';
    this.listQuery.endTime = this.dateRange[1] + ' 23:59:59';
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      await getSyslog(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    async handleSearch() {
      this.getList();
    },
    changeDate(val) {
      if (val) {
        this.listQuery.beginTime = val[0];
        this.listQuery.endTime = val[1];
      } else {
        this.listQuery.beginTime = null;
        this.listQuery.endTime = null;
      }
    }
  }
};
</script>
